﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using LynxWebLog.ViewModels;

namespace LynxWebLog.Controllers
{
    public class HomeController : Controller
    {        
        HttpClient client = ClientUtility.BlogAPIClient();

        public async Task<ActionResult> Index()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            ViewBag.HeaderImage = "~/Images/Ethernet.jpg";

            List<BlogData> _lbd = new List<BlogData>();
            try {
                if (!User.Identity.IsAuthenticated) {
                    HttpResponseMessage resp = await client.GetAsync("api/BlogData");
                    resp.EnsureSuccessStatusCode();

                    IEnumerable<BlogData> bd = await resp.Content.ReadAsAsync<IEnumerable<BlogData>>();
                    _lbd = SelectRandom(bd.ToList(), 2);
                } else {
                    HttpResponseMessage bResp = await client.GetAsync("api/BlogData/?$filter= username eq '" + User.Identity.Name + "'");
                    bResp.EnsureSuccessStatusCode();

                    _lbd = (await bResp.Content.ReadAsAsync<IEnumerable<BlogData>>()).ToList();
                }
            }
            catch (SerializationException ex) {
                Debug.Print(ex.Message);
            }
            catch (HttpRequestException ex) {
                Debug.Print(ex.Message);
            }
            ViewBag.ListBlogData = _lbd;

            List<PageDataView> _lpdv = new List<PageDataView>();
            try {
                HttpResponseMessage resp = await client.GetAsync("api/PageDataView");
                resp.EnsureSuccessStatusCode();

                List<PageDataView> pdv = await resp.Content.ReadAsAsync<List<PageDataView>>();

                if (!User.Identity.IsAuthenticated) {
                    _lpdv = SelectRandom(pdv, 2);
                } else {
                    _lpdv = (from PageDataView p in pdv where p.bd.username == User.Identity.Name select p).ToList();
                }
            }
            catch (SerializationException ex) {
                Debug.Print(ex.Message);
            }
            catch (HttpRequestException ex) {
                Debug.Print(ex.Message);
            }
            ViewBag.ListPageDataView = _lpdv;

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            ViewBag.Message = "LynxWebLog is built using Mobile First capabilities";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            ViewBag.Message = "LynxWebLog support is available from:";

            return View();
        }

        public ActionResult Privacy()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;

            return View();
        }

        public ActionResult Terms()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;

            return View();
        }

        private List<BlogData> SelectRandom(List<BlogData> input, int count)
        {
            List<BlogData> output = new List<BlogData>();
            Random random = new Random();

            if (input.Count > count) {
                foreach (BlogData item in input){
                    int rnd = random.Next(0, 10);
                    if (rnd < 8) {
                        output.Add(item);
                        if (output.Count >= count) {
                            return output;
                        }
                    }
                }
            } else {
                output = input;
            }
            return output;
        }

        private List<PageDataView> SelectRandom(List<PageDataView> input, int count)
        {
            List<PageDataView> output = new List<PageDataView>();
            Random random = new Random();

            if (input.Count > count) {
                foreach (PageDataView item in input) {
                    int rnd = random.Next(0, 10);
                    if (rnd < 8) {
                        output.Add(item);
                        if (output.Count >= count) {
                            return output;
                        }
                    }
                }
            } else {
                output = input;
            }
            return output;
        }
    }
}